#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：Employment_class 
@File    ：05_gevent服务器.py
@IDE     ：PyCharm 
@Author  ：XiaoDengYa
@Date    ：2021/7/5/005 20:28 
"""
import gevent
from gevent import socket, monkey

monkey.patch_all()  #


def handle_request(conn):
    while True:
        data = conn.recv(1024)
        if not data:
            conn.close()
            break
        print("recv:", data)
        conn.send(data)


def server(port):
    s = socket.socket()
    s.bind(("", int(port)))
    s.listen(5)
    while True:
        cli, addr = s.accept()
        gevent.spawn(handle_request, cli)


if __name__ == '__main__':
    server(7789)
